package cn.gemiman.daoyi.module.cms.dal.dataobject.appsite;

import cn.gemiman.daoyi.framework.common.util.json.JsonUtils;
import cn.gemiman.daoyi.framework.mybatis.core.dataobject.BaseDO;
import cn.gemiman.daoyi.framework.mybatis.core.type.StringListTypeHandler;
import com.baomidou.mybatisplus.annotation.KeySequence;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.handlers.AbstractJsonTypeHandler;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotNull;
import lombok.*;

import java.lang.reflect.Field;
import java.util.List;

/**
 * 站点 DO
 *
 * @author 道一源码
 */
@TableName(value = "cms_app_site", autoResultMap = true)
@KeySequence("cms_app_site_seq") // 用于 Oracle、PostgreSQL、Kingbase、DB2、H2 数据库的主键自增。如果是 MySQL 等数据库，可不写。
@Data
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class AppSiteDO extends BaseDO {

    /**
     * 编号
     */
    @TableId
    private Long id;
    /**
     * 站点类型
     * <p>
     * 枚举 {@link TODO terminal 对应的类}
     */
    private Integer terminalType;
    /**
     * 站点标识
     */
    private String appId;
    /**
     * 站点名称
     */
    private String appName;
    /**
     * LOGO
     */
    private String appLogoUrl;
    /**
     * 站点相册
     */
    @TableField(typeHandler = StringListTypeHandler.class)
    private List<String> appPhotos;
    /**
     * 站点简介
     */
    private String appShortDesc;
    /**
     * 站点介绍
     */
    private String appDescription;
    /**
     * 站点配置
     */
    @TableField(typeHandler = AppSiteConfigListTypeHandler.class)
    private List<AppSiteConfigItem> configInfoList;

    /**
     * 访问次数
     */
    private Long viewTimes;


    // @Schema(description = "点赞数量")
    private Long thumbsUpTimes = 0L;

    /**
     * 绑定域名
     */
    private String bindDomain;
    /**
     * 用户类型
     */
    private Integer userType;
    /**
     * 用户编号
     */
    private Long userId;
    /**
     * 用户昵称
     */
    private String userNickname;
    /**
     * 用户头像
     */
    private String userAvatar;
    /**
     * 评论状态
     * <p>
     * 枚举 {@link TODO common_status 对应的类}
     */
    private Integer status;
    /**
     * 审核状态
     * <p>
     * 枚举 {@link TODO bpm_process_instance_status 对应的类}
     */
    private Integer approveStatus;

    public static class AppSiteConfigListTypeHandler extends AbstractJsonTypeHandler<List<AppSiteConfigItem>> {

        public AppSiteConfigListTypeHandler(Class<?> type) {
            super(type);
        }

        public AppSiteConfigListTypeHandler(Class<?> type, Field field) {
            super(type, field);
        }

        @Override
        public List<AppSiteConfigItem> parse(String json) {
            return JsonUtils.parseArray(json, AppSiteConfigItem.class);
        }

        @Override
        public String toJson(List<AppSiteConfigItem> obj) {
            return JsonUtils.toJsonString(obj);
        }

    }

    @Schema(description = "管理后台 - 站点配置项")
    @Data
    public static class AppSiteConfigItem {
        @Schema(description = "配置项标识", requiredMode = Schema.RequiredMode.REQUIRED, example = "company_name")
        @NotNull(message = "配置项标识不能为空")
        private String key;
        @Schema(description = "配置项类型: 0-字符串，1-单张图片，2-多图，3-文件", requiredMode = Schema.RequiredMode.REQUIRED, example = "0")
        @NotNull(message = "配置项类型不能为空")
        private Integer type = 0;
        @Schema(description = "配置项值", requiredMode = Schema.RequiredMode.REQUIRED, example = "云享嘉")
        @NotNull(message = "配置项值不能为空")
        private Object value;
    }
}
